﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    /// <summary>
    /// 课程管理
    /// </summary>
    public class CourceInfoController : BaseController
    {
        private readonly ICourceService _courceService;
        private readonly ITimeTableService _timeTableService;

        public CourceInfoController(ICourceService courceService, ITimeTableService timeTableServic)
        {
            _courceService = courceService;
            _timeTableService = timeTableServic;
        }

        // GET: Course
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 创建课程页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 编辑课程
        /// </summary>
        /// <param name="id">课程ID</param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            var courcePO = _courceService.Find(id);
            return View(courcePO);
        }

        /// <summary>
        /// 课程列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 选择课程名单
        /// </summary>
        /// <returns></returns>
        public ActionResult CourceList4Teacher()
        {
            return View();
        }

        /// <summary>
        /// 保存课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveCourse(CourceEntity courceVO)
        {
            try
            {
                if (courceVO.CourceNo.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入编号！");
                }
                if (courceVO.CourceName.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入课程名称！");
                }
                _courceService.saveOrUpdateCource(courceVO);
                return Content("true");
            }
            catch (Exception e)
            {
                return Content("err:" + e.Message);
            }
        }

        /// <summary>
        /// 编辑课程
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateCourse()
        {
            return View();
        }

        /// <summary>
        /// 新增课程时间点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddCourseTime()
        {
            return View();
        }

        /// <summary>
        /// 删除课程时间点
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteCourseTime()
        {
            return View();
        }

        /// <summary>
        /// 按课程获取时间点列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult CourseTimeList(long courseId)
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetCourceList()
        {
            return Json(_courceService.LoadEntities(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetCourceList4TimeTable()
        {
            long id = 0;
            var timeTableList = _timeTableService.GetTimeTableListByTeacherId(CurrentUser.UserId);
            if (timeTableList.Any())
            {
                id = timeTableList.FirstOrDefault().Id;
            }
            return Json(_timeTableService.GetTimeTableCourceList(id), JsonRequestBehavior.AllowGet);
        }
    }
}